package geom

type numeric interface {
	int | int8 | int16 | int32 | int64 | float32 | float64
}

func Abs[T numeric](x T) T {
	var zero T
	if x < zero {
		return -x
	}

	return x
}

func Sign[T numeric](x T) T {
	var zero T
	if x < zero {
		return (T)(-1)
	} else if x > zero {
		return (T)(1)
	}

	return 0
}

func Min[T numeric](x ...T) T {
	m := x[0]
	for _, v := range x[1:] {
		if v < m {
			m = v
		}
	}
	return m
}

func triangleDoubleSquare(a Point, b Point, c Point) float64 {
	return Abs((b.X-a.X)*(c.Y-a.Y) - (c.X-a.X)*(b.Y-a.Y))
}

func CalcBendPosition(from, to Point) Point {
	dx := to.X - from.X
	dy := to.Y - from.Y
	adx := Abs(dx)
	ady := Abs(dy)
	diagonalLength := min(adx, ady)
	if diagonalLength == 0 || adx == ady {
		return from
	}
	return Point{X: from.X + Sign(dx)*diagonalLength, Y: from.Y + Sign(dy)*diagonalLength}
}
